package com.example.gybicycle.service;

import java.util.Date;
import com.example.gybicycle.common.entity.Result;
import com.example.gybicycle.common.enums.NumType;
import com.example.gybicycle.common.sql.CrudService;
import com.example.gybicycle.common.util.NumberUtil;
import com.example.gybicycle.entity.Active;
import com.example.gybicycle.dao.ActiveDao;
import com.example.gybicycle.entity.ActiveUser;
import com.example.gybicycle.entity.BikePoint;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class ActiveService extends CrudService<ActiveDao, Active> {

    public Active get(Active active) {
        Active table=dao.getOne(active);
        return table;
    }
    public Result save(Active entity,String loginId){
        int num=0;
        if(entity.getId()==null){
            entity.setState(0);             
            num=dao.insert(entity);
        }else {
            num=dao.update(entity);
        }
        if(num>0)
            return Result.success(entity);
        else 
            return Result.fail("保存失败");
    }

    @Override
    public int insert(Active entity) {
        entity.setId(NumberUtil.getNum(NumType.ACTIVE));
        int num=super.insert(entity);
        return  num;
    }

    @Override
    public int insertByList(List<Active> list) {
        for(Active entity:list){
            entity.setId(NumberUtil.getNum(NumType.ACTIVE));
        }
        return super.insertByList(list);
    }

    public List<Active> findList(Active entity){
        List<Active> list=super.findList(entity);
        return list;
    }

    public Result joinActive(String activeId,String loginId){
        try{
            ActiveUser user=new ActiveUser();
            user.setActiveId(activeId);
            user.setUserId(loginId);
            if(activeId==null||loginId==null||activeId.length()==0||loginId.length()==0)
                return Result.fail("活动ID不能为空");
            int num=dao.joinActive(user);
           if(num>0)
               return Result.success("参与成功");
           else return Result.fail("参与失败");
        }catch (Exception e){
            String msg=e.getMessage();
            if(msg.contains("Duplicate entry"))
                return Result.fail("当前用户已报名");
            return Result.error();
        }

    }


}